import AntModal from '@/components/pt/dialog/AntModal'
import SelectArea from '@/components/pt/selectArea/SelectArea'
import ImageUpload from '@/components/pt/imageUpload/ImageUpload'
import { getRegion, addRegion, updateRegion, checkCodeUnique } from '@/api/platform/region'
import { getUser } from '@/api/system/user'
import { getDept } from '@/api/system/dept'
import SelectUser from '@/components/pt/selectUser/SelectUser'
import SelectDept from '@/components/pt/selectDept/SelectDept'

export default {
  name: 'CreateForm',
  props: {
    typeOptions: {
      type: Array,
      required: true
    },
    isPrivateOptions: {
      type: Array,
      required: true
    }
  },
  components: {
    SelectArea,
    SelectUser,
    SelectDept,
    ImageUpload,
    AntModal
  },
  data() {
    const validateCode = (rule, value, callback) => {
      checkCodeUnique(this.form.id, this.form.code).then(response => {
        if (response.data) {
          return callback(new Error('地区编码不能重复'))
        }
        callback()
      })
    }
    return {
      open: false,
      closeDialog: true,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      disabled: false,
      deptDisabled: false,
      total: 0,
      id: undefined,
      formTitle: '添加地区表',
      // 表单参数
      form: {},
      rules: {
        name: [{ required: true, message: '地区名称不能为空', trigger: 'blur' }],
        abbreviation: [{ required: true, message: '地区简称不能为空', trigger: 'blur' }],
        code: [
          { required: true, message: '地区编码不能为空' },
          { validator: validateCode, trigger: 'blur' }
        ],
        province: [{ required: true, message: '请选择地区省份', trigger: 'blur' }],
        type: [{ required: true, message: '请选择地区类型', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,

        code: undefined,

        avatar: undefined,

        abbreviation: undefined,

        province: undefined,

        userId: undefined,

        departId: undefined,

        momentImage: undefined,

        officialAccountImage: undefined,

        receiveMoneyImage: undefined,

        type: undefined,

        isPrivate: '0',

        status: '0'
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.formTitle = '添加地区表'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const regionId = row.id
      getRegion(regionId).then(response => {
        this.form = response.data
        if (this.$store.state.user.roles[0] === 'admin') {
          this.disabled = true
          if (this.form.userId !== null) {
            getUser(this.form.userId).then(res => {
              this.form.userId = { ids: this.form.userId, names: res.data.name }
            })
          }
          if (this.form.departId !== null) {
            getDept(this.form.departId).then(res => {
              this.form.departId = { ids: this.form.departId, names: res.data.deptName }
            })
          }
        }
        this.formTitle = '修改地区表'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function (closeDialog) {
      this.closeDialog = closeDialog
      this.disabled = true
      this.$refs.form.validate(valid => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form))
          if (this.$store.state.user.roles[0] === 'admin') {
            if (this.form.userId !== null && this.form.userId !== undefined) {
              saveForm.userId = this.form.userId.ids
            }
            if (this.form.departId !== null && this.form.departId !== undefined) {
              saveForm.departId = this.form.departId.ids
            }
          }
          if (this.form.id !== undefined) {
            updateRegion(saveForm).then(response => {
              this.$message.success('更新成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          } else {
            addRegion(saveForm).then(response => {
              this.$message.success('新增成功', 3)
              this.open = false
              this.$emit('ok')
              this.$emit('close')
              this.disabled = false
            })
          }
        } else {
          this.disabled = false
          return false
        }
      })
    },
    back() {
      const index = '/platform/region/index'
      this.$router.push(index)
    }
  }
}
